//
//  UIViewController+NEDRouter.m
//  NEDRoute
//
//  Created by 000 on 2022/2/8.
//

#import "UIViewController+NEDRouter.h"
#import "NEDRouter.h"


@implementation UIViewController (NEDRouter)




- (UIViewController *)ned_push{
    [NEDRouter ned_pushViewController:self];
    return self;
}

- (UIViewController *)ned_pop{
    [self.navigationController popViewControllerAnimated:YES];
    return self;
}

- (UIViewController *)ned_present{
    [NEDRouter ned_presentViewController:self];
    return self;
}

- (UIViewController *)ned_dismiss{
    [self dismissViewControllerAnimated:YES completion:nil];
    return self;
}

/// 把该控制器 从他所在的导航条控制器中移除掉
- (void)ned_destroyFromNavigationDelay{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        SEL rtNavigation = @selector(rt_navigationController);
        if([self respondsToSelector:rtNavigation]){
            UINavigationController * navc = [self performSelector:rtNavigation];
            if([navc isKindOfClass:NSClassFromString(@"RTRootNavigationController")]){
                SEL removeSEL = @selector(removeViewController:);
                if([navc respondsToSelector:removeSEL]){
                    [navc performSelector:removeSEL withObject:self];
                }
            }
        }else{
            NSMutableArray * childList = [NSMutableArray arrayWithArray:self.navigationController.childViewControllers];
            if(![childList containsObject:self]){
                return;
            }
            [childList removeObject:self];
            [self.navigationController setViewControllers:childList animated:NO];
        }
    });
}

@end
